package com.lagou.sqlSession;

import com.lagou.pojo.Configuration;
import com.lagou.pojo.MappedStatement;

import java.lang.reflect.*;
import java.util.List;

/**
 * @author lubiao
 * @date 2021/4/2
 * @desc
 */
public class DefaultSqlSession implements SqlSession {

    private Configuration configuration;

    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public <E> List<E> selectList(String statementId,Object... params) throws Exception {

        //将要去完成对simpleExecute里的query方法的调用
        Executor simpleExecute = new SimpleExecute();
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        List<Object> list = simpleExecute.query(configuration, mappedStatement, params);
        return (List<E>) list;
    }

    @Override
    public <T> T selectOne(String statementId, Object... params) throws Exception {
        List<Object> objects = selectList(statementId,params);
        if (objects.size() == 1) {
            return (T) objects.get(0);
        }else{
            throw new RuntimeException("查询结果为空或者结果为多个");
        }
    }

    @Override
    public <T> T getMapper(Class<?> mapperClass) {

        //使用JDK动态代理来为dao接口生成代理对象，并返回
        Object proxyInstance = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class[]{mapperClass}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

                String methodName = method.getName();
                String namespace = method.getDeclaringClass().getName();
                String statementId = namespace + "." + methodName;

                Type genericReturnType = method.getGenericReturnType();
                String typeName = genericReturnType.getTypeName();
                if(genericReturnType instanceof ParameterizedType){
                    List<Object> objects = selectList(statementId, args);
                    return objects;
                }else if("java.lang.Integer".equals(typeName)){
                    return update(statementId,args);
                }
                return selectOne(statementId,args);
            }
        });

        return (T) proxyInstance;
    }

    @Override
    public Integer add(String statementId, Object... params) throws Exception {
        return update(statementId,params);
    }

    @Override
    public Integer delete(String statementId, Object... params) throws Exception {
        return update(statementId,params);
    }


    @Override
    public Integer update(String statementId, Object... params) throws Exception {
        //将要去完成对simpleExecute里的update方法的调用
        Executor simpleExecute = new SimpleExecute();
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        Integer updateCount = simpleExecute.update(configuration, mappedStatement, params);
        return updateCount;
    }


}
